// 创建一个真正的WAV音频文件用于测试
const fs = require('fs');
const path = require('path');

// 生成一个简单的WAV文件（包含1秒的440Hz正弦波）
function createProperWavFile() {
    const filePath = path.join(__dirname, 'proper-test-audio.wav');
    
    // WAV文件头信息
    const sampleRate = 44100;
    const duration = 1; // 1秒
    const numSamples = sampleRate * duration;
    const amplitude = 0.5;
    const frequency = 440; // A音
    
    // 创建音频数据
    const audioData = new Float32Array(numSamples);
    for (let i = 0; i < numSamples; i++) {
        audioData[i] = amplitude * Math.sin(2 * Math.PI * frequency * i / sampleRate);
    }
    
    // 创建WAV文件
    const buffer = new ArrayBuffer(44 + numSamples * 2);
    const view = new DataView(buffer);
    
    // RIFF header
    writeString(view, 0, 'RIFF');
    view.setUint32(4, 36 + numSamples * 2, true);
    writeString(view, 8, 'WAVE');
    
    // fmt chunk
    writeString(view, 12, 'fmt ');
    view.setUint32(16, 16, true); // chunk size
    view.setUint16(20, 1, true); // PCM format
    view.setUint16(22, 1, true); // mono
    view.setUint32(24, sampleRate, true);
    view.setUint32(28, sampleRate * 2, true); // byte rate
    view.setUint16(32, 2, true); // block align
    view.setUint16(34, 16, true); // bits per sample
    
    // data chunk
    writeString(view, 36, 'data');
    view.setUint32(40, numSamples * 2, true);
    
    // 写入音频数据
    let index = 44;
    for (let i = 0; i < numSamples; i++) {
        const sample = Math.max(-1, Math.min(1, audioData[i]));
        view.setInt16(index, sample * 0x7FFF, true);
        index += 2;
    }
    
    // 写入文件
    const buf = Buffer.from(buffer);
    fs.writeFileSync(filePath, buf);
    
    console.log(`正确的WAV音频文件已创建: ${filePath}`);
    console.log(`文件大小: ${fs.statSync(filePath).size} 字节`);
}

function writeString(view, offset, string) {
    for (let i = 0; i < string.length; i++) {
        view.setUint8(offset + i, string.charCodeAt(i));
    }
}

// 执行创建
createProperWavFile();